package ifmo.group5512.dao.impl;


import ifmo.group5512.dao.IOrderDAO;
import ifmo.group5512.entity.Order;
import ifmo.group5512.entity.OrderProductTable;
import ifmo.group5512.entity.Product;
import ifmo.group5512.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class OrderDAO implements IOrderDAO{
   
	public void saveOrder(OrderProductTable order){
		 Session session = null;
		    try {
		      session = HibernateUtil.getSessionFactory().openSession();
		      session.beginTransaction();
		      session.save(order);
		      
		      session.getTransaction().commit();
		    } catch (Exception e) {
		      // ignore
		    } finally {
		      if (session != null && session.isOpen()) {

		        session.close();
		      }
		    }
		
	}
	
	public Collection getAllOrders(){
        Session session = null;
        List orders = new ArrayList();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
//            String SQL_QUERY ="select * from orderproducttable inner join product on orderproducttable.productid=product.id " +
//            		"inner join order_ on order_.Id = orderproducttable.orderId;" ;
//
//  			Query query = session.createSQLQuery(SQL_QUERY).addEntity(OrderProductTable.class);
 			 
// 			orders = query.list();
            orders = session.createCriteria(OrderProductTable.class).list();
 			System.out.println( ((OrderProductTable) orders.get(0)).getOrders().getBeginDate());
 			
            session.getTransaction().commit();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (session != null && session.isOpen()) {

                session.close();
            }
        }
        return orders;
    }
    public Integer getComplexityForOrder(Order order){
        /*Integer id = order.getId();
        Session session = null;
        List<Integer> complexityList = new ArrayList<>();
        Integer totalComplexity = 0;
        
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            String hql = "Select ComplexityOfManufacturing from Product" +
                    " as a inner join OrderProductTable as b on a.Id = b.productId" +
                    " join Order as c on b.id = c.OrderStateId where c.id = :id";
            Query query = session.createQuery(hql).setInteger("id",id);
            
            complexityList = query.list();

            session.getTransaction().commit();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (session != null && session.isOpen()) {

                session.close();
            }
        }
        
        for(Integer complexity: complexityList){
             totalComplexity+=complexity;
        }
        return totalComplexity;*/
        return 1;
    }
    public Order getOrderById(Integer id){
    	Session session = null;
        Order order = null;
        try {
          session = HibernateUtil.getSessionFactory().openSession();
          session.beginTransaction();
          
          order = (Order) session.load(Order.class, id);
         // order.getBeginDate()
          session.getTransaction().commit();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (session != null && session.isOpen()) {

                session.close();
            }
        }
        return order;
    }
    
    public Product getProductById(Integer id){
    	Session session = null;
        Product product = null;
        try {
          session = HibernateUtil.getSessionFactory().openSession();
          session.beginTransaction();
          
          product = (Product) session.load(Product.class, id);
         // order.getBeginDate()
          session.getTransaction().commit();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (session != null && session.isOpen()) {

                session.close();
            }
        }
        return product;
    }
    
    public Integer getDaysToComplete(Order order){ return 15;}
    public List getNeededMaterials(Order order){return new ArrayList();}
    public Integer getWaitingForMaterials(Order order){return 5;}
    public void setNewOrdersQueue(List<Order> orders){
        //do nothing lol
    }
}
